//
//  HZJSessionManager.swift
//  test
//
//  Created by Apple on 2016/11/6.
//  Copyright © 2016年 xiaomage. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType {
    case Get
    case Post
}

class HZJSessionManager: NSObject {

    static let shareInstance = HZJSessionManager()
    
    lazy var manager = {
      
        return $0
    }(AFHTTPSessionManager())
    
    func request(method: RequestType, urlStr: String, parameters: [String: Any], result:@escaping (_ response: [String: Any]?, _ error: String?) -> ()) {
        
        let successBlock = { (task: URLSessionDataTask, response: Any?) in
            result(response as? [String: Any], nil)
        }
        
        let failureBlock = { (task: URLSessionDataTask?, error: Error) in
            result(nil, error.localizedDescription)
        }
        
        
        if method == .Get {

            manager.get(urlStr, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
            
        }else if method == .Post {
            
            manager.post(urlStr, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
        
    }
}
